package com.pokeoffice.gui;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import com.pokeoffice.client.Client;
import com.pokeoffice.client.Session;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * The Class Login.
 */
public class Login extends JPanel {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -964802509489946923L;

	/** The txt username. */
	private JTextField txtUsername;

	/** The password field. */
	private JPasswordField passwordField;

	/** The session. */
	private Session session;

	/**
	 * Instantiates a new login.
	 * 
	 * @param session
	 *            the session
	 */
	public Login(final Session session) {
		this.session = session;

		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);

		txtUsername = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, txtUsername, 275,
				SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, txtUsername, 150,
				SpringLayout.WEST, this);
		txtUsername.setToolTipText("Username");
		add(txtUsername);
		txtUsername.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setToolTipText("Password");
		passwordField.setColumns(10);
		springLayout.putConstraint(SpringLayout.NORTH, passwordField, 15,
				SpringLayout.SOUTH, txtUsername);
		springLayout.putConstraint(SpringLayout.WEST, passwordField, 0,
				SpringLayout.WEST, txtUsername);
		add(passwordField);

		JLabel lblUsername = new JLabel("Username");
		springLayout.putConstraint(SpringLayout.NORTH, lblUsername, 2,
				SpringLayout.NORTH, txtUsername);
		springLayout.putConstraint(SpringLayout.WEST, lblUsername, 15,
				SpringLayout.EAST, txtUsername);
		add(lblUsername);

		JLabel lblPassword = new JLabel("Password");
		springLayout.putConstraint(SpringLayout.NORTH, lblPassword, 2,
				SpringLayout.NORTH, passwordField);
		springLayout.putConstraint(SpringLayout.EAST, lblPassword, 0,
				SpringLayout.EAST, lblUsername);
		add(lblPassword);

		JButton btnLogin = new JButton("Login");

		btnLogin.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				session.login(txtUsername.getText(), passwordField
						.getPassword());
				passwordField.setText("");
			}

		});

		springLayout.putConstraint(SpringLayout.NORTH, btnLogin, 15,
				SpringLayout.SOUTH, passwordField);
		springLayout.putConstraint(SpringLayout.WEST, btnLogin, 0,
				SpringLayout.WEST, txtUsername);
		springLayout.putConstraint(SpringLayout.EAST, btnLogin, 0,
				SpringLayout.EAST, txtUsername);
		add(btnLogin);

		ImagePanel panel = new ImagePanel(Client.welcome);
		add(panel);

		Dimension size = new Dimension(Client.WIDTH, Client.HEIGHT);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
	}
}
